package com.yanqu.road.dao.impl.activity.chefduel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.autodata.chefduel.ChefDuelPhase1UserData;
import com.yanqu.road.entity.autodata.chefduel.entity.ChefDuel1DeviceGroup;
import com.yanqu.road.entity.autodata.chefduel.entity.ChefDuel1StageData;
import org.apache.mina.util.ConcurrentHashSet;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChefDuelPhase1UserDataDaoImpl extends TempDataObjectDao<ChefDuelPhase1UserData> {
    public static String getChefDuelPhase1UserDataSql = "select * from `t_u_chef_duel_phase1_user` where `activityId`=? and `userId`=?";
    public ChefDuelPhase1UserData getChefDuelPhase1UserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getChefDuelPhase1UserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateChefDuelPhase1UserDataSql = "update `t_u_chef_duel_phase1_user` set energyNum=?,lastRecoveryEnergyTime=?,deviceDataMap=?,remainPoint=?,totalGetPoint=?,skill=?,grade=?,highestGrade=?,passStages=?,totalModifyTime=?,score=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(ChefDuelPhase1UserData chefDuelPhase1UserData) {
        chefDuelPhase1UserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, chefDuelPhase1UserData.getEnergyNum()));
        para.put(i++, new DbParameter(Types.BIGINT, chefDuelPhase1UserData.getLastRecoveryEnergyTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(chefDuelPhase1UserData.getDeviceDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, chefDuelPhase1UserData.getRemainPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, chefDuelPhase1UserData.getTotalGetPoint()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(chefDuelPhase1UserData.getSkill())));
        para.put(i++, new DbParameter(Types.BIGINT, chefDuelPhase1UserData.getGrade()));
        para.put(i++, new DbParameter(Types.BIGINT, chefDuelPhase1UserData.getHighestGrade()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(chefDuelPhase1UserData.getPassStages())));
        para.put(i++, new DbParameter(Types.INTEGER, chefDuelPhase1UserData.getTotalModifyTime()));
        para.put(i++, new DbParameter(Types.BIGINT, chefDuelPhase1UserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, chefDuelPhase1UserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, chefDuelPhase1UserData.getUserId()));
        if (getDb().executeNoneQuery(updateChefDuelPhase1UserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertChefDuelPhase1UserDataSql = "insert into `t_u_chef_duel_phase1_user`(activityId,userId,energyNum,lastRecoveryEnergyTime,deviceDataMap,remainPoint,totalGetPoint,skill,grade,highestGrade,passStages,totalModifyTime,score) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ChefDuelPhase1UserData chefDuelPhase1UserData) {
        chefDuelPhase1UserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, chefDuelPhase1UserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, chefDuelPhase1UserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, chefDuelPhase1UserData.getEnergyNum()));
        para.put(i++, new DbParameter(Types.BIGINT, chefDuelPhase1UserData.getLastRecoveryEnergyTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(chefDuelPhase1UserData.getDeviceDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, chefDuelPhase1UserData.getRemainPoint()));
        para.put(i++, new DbParameter(Types.INTEGER, chefDuelPhase1UserData.getTotalGetPoint()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(chefDuelPhase1UserData.getSkill())));
        para.put(i++, new DbParameter(Types.BIGINT, chefDuelPhase1UserData.getGrade()));
        para.put(i++, new DbParameter(Types.BIGINT, chefDuelPhase1UserData.getHighestGrade()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(chefDuelPhase1UserData.getPassStages())));
        para.put(i++, new DbParameter(Types.INTEGER, chefDuelPhase1UserData.getTotalModifyTime()));
        para.put(i++, new DbParameter(Types.BIGINT, chefDuelPhase1UserData.getScore()));
        if (getDb().executeNoneQuery(insertChefDuelPhase1UserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ChefDuelPhase1UserData parseData(ResultSet rs) throws SQLException {
        ChefDuelPhase1UserData chefDuelPhase1UserData = new ChefDuelPhase1UserData();
        chefDuelPhase1UserData.setActivityId(rs.getInt("activityId"));
        chefDuelPhase1UserData.setUserId(rs.getLong("userId"));
        chefDuelPhase1UserData.setEnergyNum(rs.getInt("energyNum"));
        chefDuelPhase1UserData.setLastRecoveryEnergyTime(rs.getLong("lastRecoveryEnergyTime"));
        chefDuelPhase1UserData.setDeviceDataMap(JSON.parseObject(rs.getString("deviceDataMap"), new TypeReference<Map<Integer, ChefDuel1DeviceGroup>>() {}));
        chefDuelPhase1UserData.setRemainPoint(rs.getInt("remainPoint"));
        chefDuelPhase1UserData.setTotalGetPoint(rs.getInt("totalGetPoint"));
        chefDuelPhase1UserData.setSkill(JSON.parseObject(rs.getString("skill"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        chefDuelPhase1UserData.setGrade(rs.getLong("grade"));
        chefDuelPhase1UserData.setHighestGrade(rs.getLong("highestGrade"));
        chefDuelPhase1UserData.setPassStages(JSON.parseObject(rs.getString("passStages"), new TypeReference<ConcurrentHashMap<Integer, ChefDuel1StageData>>() {}));
        chefDuelPhase1UserData.setTotalModifyTime(rs.getInt("totalModifyTime"));
        chefDuelPhase1UserData.setScore(rs.getLong("score"));

        return chefDuelPhase1UserData;
    }
}
